package com.hejia.alauda.service;

import com.hejia.alauda.Constants;
import com.hejia.alauda.entity.SaleActivity;
import com.hejia.alauda.entity.SaleActivity.Tag;
import com.hejia.alauda.entity.SaleActivityDevice;
import com.hejia.alauda.entity.example.SaleActivityDeviceExample;
import com.hejia.alauda.entity.example.SaleActivityExample;
import com.hejia.alauda.persistence.mybatis.SaleActivityDeviceMapper;
import com.hejia.alauda.persistence.mybatis.SaleActivityMapper;
import com.hejia.alauda.utils.Pager;
import com.hejia.alauda.utils.Utils;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * Created by jimi on 14/12/3.
 */
@Service
public class SaleActivityServiceImpl implements SaleActivityService {

	@Resource
	private SaleActivityMapper mapper;

	@Resource
	private SaleActivityDeviceMapper saleActivityDeviceMapper;

	@Override
	public Pager<SaleActivity> pagerSaleActivity(Pager<SaleActivity> pager) {
		SaleActivityExample example = new SaleActivityExample();
		example.appendCriterion("available = ", 1);
		example.setOrderByClause("isFinish,`orderBy` desc,createDate");
		List<SaleActivity> list = mapper.selectByExample(example, pager);
		pager.setTotalCount(mapper.countByExample(example));
		pager.setList(list);
		return pager;
	}

	@Override
	public boolean insertEntity(SaleActivity saleActivity) {
		saleActivity.setCreateDate(new Date());
		return mapper.insert(saleActivity) > 0;
	}

	@Override
	public boolean updateEntity(SaleActivity saleActivity) {
		saleActivity.setModifyDate(new Date());
		return mapper.updateByPrimaryKey(saleActivity) > 0;
	}

	@Override
	public SaleActivity findSaleActivityById(Long id) {
		SaleActivityExample example = new SaleActivityExample();
		example.appendCriterion("id = ", id);
		example.appendCriterion("available = ", 1);
		return mapper.selectOneByExample(example);
	}

	@Override
	public boolean upSaleActivity(Long id) {
		SaleActivity saleActivity = mapper.selectByPrimaryKey(id);
		saleActivity.setStatus(SaleActivity.Status.ONLINE);
		saleActivity.setModifyDate(new Date());
		return mapper.updateByPrimaryKey(saleActivity) > 0;
	}

	@Override
	public boolean downSaleActivity(Long id) {
		SaleActivity saleActivity = mapper.selectByPrimaryKey(id);
		saleActivity.setStatus(SaleActivity.Status.OFFLINE);
		saleActivity.setModifyDate(new Date());
		return mapper.updateByPrimaryKey(saleActivity) > 0;
	}

	@Override
	public boolean updateSaleActivityDisable(Long id) {
		SaleActivity activity = new SaleActivity();
		activity.setId(id);
		activity.setAvailable(0l);
		SaleActivityExample example = new SaleActivityExample();
		example.appendCriterion("id = ", id);
		return mapper.updateByExample(activity, example) > 0;
	}

	@Override
	public List<SaleActivity> effectiveActivity() {
		SaleActivityExample example = new SaleActivityExample();
		example.appendCriterion("available = ", 1);
		example.appendCriterion("status = ", SaleActivity.Status.ONLINE);
		example.appendCriterion("startTime <=now() ");
		example.appendCriterion("endTime >=now() ");
		example.setOrderByClause("createDate desc");
		Pager<SaleActivity> pager = new Pager<SaleActivity>();
		pager.setPageSize(Pager.MAX_PAGE_SIZE);
		return mapper.selectByExample(example, pager);
	}

	@Override
	public SaleActivity findEffectiveActivityById(Long id) {
		SaleActivityExample example = new SaleActivityExample();
		example.appendCriterion("id = ", id);
		example.appendCriterion("available = true");
		example.appendCriterion("status = ", SaleActivity.Status.ONLINE);
		example.appendCriterion(" (overdueHide=false or ( startTime<=now() and endTime>=now()))");
		example.setOrderByClause("createDate desc");
		return mapper.selectOneByExample(example);
	}

	@Override
	public Pager<SaleActivity> activityCenter(Pager<SaleActivity> pager,
			String os) {
		mapper.updateSaleActivityState();

		SaleActivityExample example = new SaleActivityExample();
		example.appendCriterion("displayInCenter = true");
		example.appendCriterion("available = ", 1);
		example.appendCriterion("startTime<now()");
		if (Constants.PLATFORM_IOS.equalsIgnoreCase(os)) {
			example.appendCriterion(" iOSBanner is not null and iOSBanner != '' ");
		} else if (Constants.PLATFORM_ANDORID.equalsIgnoreCase(os)) {
			example.appendCriterion(" androidBanner is not null and androidBanner != '' ");
		} else if (Constants.PLATFORM_H5.equalsIgnoreCase(os)) {
			example.appendCriterion(" h5Banner is not null and h5Banner != '' ");
		} else {
			example.appendCriterion(" pcBanner is not null and pcBanner != '' ");
		}
		example.appendCriterion("status = ", SaleActivity.Status.ONLINE);
		example.setOrderByClause("`orderBy` desc, createDate");
		List<SaleActivity> list = mapper.selectByExample(example, pager);
		pager.setTotalCount(mapper.countByExample(example));
		pager.setList(list);
		return pager;
	}

	@Override
	public Pager<SaleActivity> displayBanner(Pager<SaleActivity> pager,
			String os) {
		mapper.updateSaleActivityState();

		SaleActivityExample example = new SaleActivityExample();
		example.appendCriterion("displayOnBanner = ", true);
		example.appendCriterion("available = ", 1);
		example.appendCriterion("isFinish =", false);
		example.appendCriterion("startTime<"
				+ StringUtils.quote(Utils.formatDateTime(new Date())));
		if (Constants.PLATFORM_IOS.equalsIgnoreCase(os)) {
			example.appendCriterion(" iOSBanner is not null and iOSBanner != '' ");
		} else if (Constants.PLATFORM_ANDORID.equalsIgnoreCase(os)) {
			example.appendCriterion(" androidBanner is not null and androidBanner != '' ");
		} else if (Constants.PLATFORM_H5.equalsIgnoreCase(os)) {
			example.appendCriterion(" h5Banner is not null and h5Banner != '' ");
		} else {
			example.appendCriterion(" pcBanner is not null and pcBanner != '' ");
		}
		example.appendCriterion("status = ", SaleActivity.Status.ONLINE);
		example.setOrderByClause("`orderBy` desc, createDate");
		List<SaleActivity> list = mapper.selectByExample(example, pager);
		pager.setTotalCount(mapper.countByExample(example));
		pager.setList(list);
		return pager;
	}

	@Override
	public Pager<SaleActivity> findActivity(SaleActivityExample example,
			Pager<SaleActivity> pager) {
		example.setOrderByClause("createDate desc");
		pager.setList(this.mapper.selectByExample(example, pager));
		pager.setTotalCount(this.mapper.countByExample(example));
		return pager;
	}

	@Override
	public Pager<SaleActivity> findActivity(Tag tag, Pager<SaleActivity> pager) {
		Map<String, Object> map = new HashMap<String, Object>();

		if (tag != null) {
			map.put("tag", tag);
		}
		map.put("startNumber", pager.getStartNumber());
		map.put("pageSize", pager.getPageSize());

		pager.setList(this.mapper.selectActivity(map));
		pager.setTotalCount(this.mapper.selectActivityCount(map));

		return pager;
	}

	@Override
	public List<SaleActivityDevice> getSaleActivityDeviceById(Long id) {
		SaleActivityDeviceExample exp = new SaleActivityDeviceExample();
		exp.appendCriterion("saleactivity_id =", id);
		return saleActivityDeviceMapper.selectByExample(exp, null);
	}

	@Override
	public boolean addSaleActivityDevice(SaleActivityDevice saleActivityDevice) {
		return saleActivityDeviceMapper.insert(saleActivityDevice) > 0;
	}

	@Override
	public boolean deleteSaleActivityDevice(
			SaleActivityDevice saleActivityDevice) {
		return saleActivityDeviceMapper.deleteByPrimaryKey(saleActivityDevice) > 0;
	}

	@Override
	public SaleActivityDevice findSaleActivityDeviceById(Long id) {
		return saleActivityDeviceMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean modifySaleActivityDevice(
			SaleActivityDevice saleActivityDevice) {
		return saleActivityDeviceMapper.updateByPrimaryKey(saleActivityDevice) > 0;
	}

	@Override
	public boolean deleteSaleActivityDeviceBySAId(Long id) {
		return saleActivityDeviceMapper.deleteSaleActivityDeviceBySAId(id) > 0;
	}

}
